#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
    // Load the image
    Mat image = imread("/home/linux/Documents/VscodeOpencv/1111.png", IMREAD_COLOR);
    if (image.empty())
    {
        cout << "Couldn't load the image" << endl;
        return -1;
    }

    // Convert the image to HSV color space
    Mat hsv;
    cvtColor(image, hsv, COLOR_BGR2HSV);

    // Define the red color range in HSV
    Scalar lower_red1 = Scalar(0, 50, 50);
    Scalar upper_red1 = Scalar(10, 255, 255);
    Scalar lower_red2 = Scalar(160, 50, 50);
    Scalar upper_red2 = Scalar(179, 255, 255);

    // Threshold the HSV image to get red regions
    Mat red_mask;
    inRange(hsv, lower_red1, upper_red1, red_mask);
    inRange(hsv, lower_red2, upper_red2, red_mask);

    // Find contours in the red mask
    vector<vector<Point>> contours;
    findContours(red_mask, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

    // Draw rectangles around the contours
    for (const auto &contour : contours)
    {
        Rect bounding_rect = boundingRect(contour);
        rectangle(image, bounding_rect, Scalar(0, 255, 0), 2);
    }

    // Display the result
    imshow("Result", image);
    waitKey(0);
    return 0;
}
